import { isEmpty } from './basic'

export const mapKeys = function (source, target = {}, map) {
    if (!map) return target;
    if (Array.isArray(map)) {
        map.forEach(function (key) {
            if (typeof key === 'string' && !isEmpty(source[key])) {
                target[key] = source[key];
                delete source[key];
            } else if (Array.isArray(key) && key.length > 1 && typeof key[0] === 'string' && typeof key[1] === 'string' && !isEmpty(source[key[0]])) {
                target[key[1]] = source[key[0]];
                delete source[key];
            }
        })
    } else if (Object.prototype.toString.call(map) === '[object Object]') {
        Object.keys(map).forEach(function (key) {
            if (!isEmpty(source[key])) {
                target[typeof map[key] === 'string' ? map[key] : key] = source[key];
                delete source[key];
            }
        })
    } else if (typeof map === 'boolean' && map === true) {
        Object.keys(source).forEach(key => {
            if (!isEmpty(source[key])) {
                target[key] = source[key];
                delete source[key];
            }
        })
    }
    return target
}

export {
    isEmpty
}
